%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Jun Yang (junyang@cs.duke.edu)

%----------------------------------------------------------------------
% General page layout:
%
% Usage:
%
%\usepackage[total={6.5in,9.0in},centering]{geometry}

%----------------------------------------------------------------------
% Font tweaks:

% Use times:
%
%\usepackage{times}
%\renewcommand{\ttdefault}{cmtt}
%\renewcommand{\sfdefault}{cmss}
% Note: always use computer modern typewriter fonts (which looks
% better than courier, for example, when using the times option).

% Desperate font size tweak:
%
% The first parameter is the font size to switch to (in pt); the
% second parameter is the \baselineskip to use.
%
%\renewcommand{\normalsize}{\fontsize{9.75}{11}\selectfont}

%----------------------------------------------------------------------
% Spacing tweaks:

% Interline spacing tweak:
%
%\renewcommand{\baselinestretch}{1.1}

% Float/text spacing tweak:
%
% Use \tightfloats in preamble to change the setting for the entire
% document.
%
\newcommand{\tightfloats}{
  % Space left between floats:
  \setlength{\floatsep}{1ex}
  % Space between last top float or first bottom float and the text:
  \setlength{\textfloatsep}{1ex}
  % Space left on top and bottom of an in-text float:
  \setlength{\intextsep}{1ex}
  % For two-column output:
  \setlength{\dblfloatsep}{\floatsep}
  \setlength{\dbltextfloatsep}{\textfloatsep}
  % Space around caption:
  \setlength{\abovecaptionskip}{0ex}
  \setlength{\belowcaptionskip}{0ex}
  % Allow fitting large figures with minimal text on a page:
  \renewcommand{\topfraction}{1.00}
  \renewcommand{\bottomfraction}{1.00}
  \renewcommand{\floatpagefraction}{1.00}
  \renewcommand{\dbltopfraction}{1.00}
  \renewcommand{\dblfloatpagefraction}{1.00}
  \renewcommand{\textfraction}{0.0}
}
\newcommand{\loosefloats}{
  % Space left between floats:
  \setlength{\floatsep}{2ex}
  % Space between last top float or first bottom float and the text:
  \setlength{\textfloatsep}{3ex}
  % Space left on top and bottom of an in-text float:
  \setlength{\intextsep}{2ex}
  % For two-column output:
  \setlength{\dblfloatsep}{\floatsep}
  \setlength{\dbltextfloatsep}{\textfloatsep}
  % Space around caption:
  \setlength{\abovecaptionskip}{0.5ex}
  \setlength{\belowcaptionskip}{0.5ex}
  % Allow fitting large figures with minimal text on a page:
  \renewcommand{\topfraction}{1.00}
  \renewcommand{\bottomfraction}{1.00}
  \renewcommand{\floatpagefraction}{1.00}
  \renewcommand{\dbltopfraction}{1.00}
  \renewcommand{\dblfloatpagefraction}{1.00}
  \renewcommand{\textfraction}{0.0}
}

\RequirePackage{titlesec}

% By default \subparagraph is textbf, which looks too similar to
% \paragraph.  We make it italic and underlined to differentiate it:
%
\let\oldsubparagraph=\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{\ul{\normalfont\normalsize\itshape#1}}}

% Section heading spacing tweaks:
%
% Use \tightsections in preamble to change the setting for the entire
% document.
%
\newcommand{\TweakSectionSpacing}[6]{
  % Note on \titlespacing*: The first parameter specifies the
  % particular section command to tweak; the second parameter
  % specifies the extra space before title (negative means the title
  % overhangs); the third parameter specifies the vertical space
  % before the title; the fourth parameter specifies either the
  % vertical space or the horizontal space between title and following
  % text.
  \titlespacing*{\section}{0em}{#1}{#6}
  \titlespacing*{\subsection}{0em}{#2}{#6}
  \titlespacing*{\subsubsection}{0em}{#3}{#6}
  \titlespacing*{\paragraph}{0em}{#4}{1em plus 0.1em}
  \titlespacing*{\subparagraph}{\parindent}{#5}{1em plus 0.1em}
}
\newcommand{\TweakSectionSpacingX}[6]{
  \newskip\ssa \ssa=#1 \advance \ssa by -\parskip
  \newskip\ssb \ssb=#2 \advance \ssb by -\parskip
  \newskip\ssc \ssc=#3 \advance \ssc by -\parskip
  \newskip\ssd \ssd=#4
  \newskip\sse \sse=#5
  \newskip\ssf \ssf=#6 \advance \ssf by -\parskip
  \TweakSectionSpacing{\ssa}{\ssb}{\ssc}{\ssd}{\sse}{\ssf}
}
\newcommand{\tightsections}{
  \TweakSectionSpacingX{2ex}{1.5ex}{1ex}{0.5ex}{0.5ex}{1ex}
}
\newcommand{\loosesections}{
  \TweakSectionSpacingX{4.0ex}{3.0ex}{2.5ex}{0.5ex}{0.5ex}{1ex}
}

% Compact lists:
%
% Use \denselists and \normallists to change the setting for the rest of
% the document.
%
\RequirePackage{macros/tweaklist}
\newcommand{\denselists}{%
  \renewcommand{\itemhook}{%
    \setlength{\topsep}{0.5ex}%
    \setlength{\leftmargin}{3ex}%
    \setlength{\partopsep}{0ex}%
    \setlength{\parsep}{\parskip}%
    \setlength{\itemsep}{0.5ex}%
  }%
  \renewcommand{\enumhook}{\itemhook}%
  \renewcommand{\descripthook}{\itemhook}%
}
\newcommand{\normallists}{%
  \renewcommand{\itemhook}{}%
  \renewcommand{\enumhook}{}%
  \renewcommand{\descripthook}{}%
}

%----------------------------------------------------------------------
% Bibs:

\RequirePackage{url}

\RequirePackage[numbers]{natbib}
\renewcommand{\cite}{\citep}

% Use the following command to adjust spacing between bib entries:
\setlength{\bibsep}{0ex}

% Use one of the following bst options instead of the standard ones:
%\bibliographystyle{plainnat}
%\bibliographystyle{abbrvnat}

%----------------------------------------------------------------------
% Figures:

\RequirePackage{ifpdf}

% Basic figures:
%
% Usage: \includegraphics{width=0.4\textwidth,angle=90}{figs/???.???}
% Works for jpg, eps, pdf, etc.  angle=90 is another useful option.

% Support for xfig:
%
% Usage: \centering\xfig{0.4\textwidth}{!}{figs/filename} where "!"
% means that the height is scaled proportionally, and figs/filename is
% the name of the xfig file (without the .xfig suffix).
%
\RequirePackage{graphicx,color}
\RequirePackage[usenames,dvipsnames,svgnames,table]{xcolor}
\ifpdf
\newcommand{\xfig}[3]{\resizebox{#1}{#2}{\input{#3.pdf_t}}}
\else
\newcommand{\xfig}[3]{\resizebox{#1}{#2}{\input{#3.pstex_t}}}
\fi

% Make text float around text:
\RequirePackage{wrapfig}
% Usage example: TO BE COMPLETED.

%----------------------------------------------------------------------
% Math, algorithms, and theorems:

\RequirePackage{amsmath}
% Usage Notes:
%
% \text{...} typesets plain text in math display.
%
% \DeclareMathOperator TO BE COMPLETED
%
% Math display environments...  TO BE COMPLETED

% Use ams math fonts:
\RequirePackage{exscale,amssymb}
\renewcommand{\emptyset}{\ensuremath{\varnothing}}

\newcommand{\defeq}{\ensuremath{\stackrel{\mbox{{\tiny def}}}{=}}}

% Enhanced theorem-like environments and proof:
\RequirePackage{amsthm}
%
% \begin{proof}[Proof Sketch]...\end{proof} typesets a proof.  [...],
% if given, will be used in place of "Proof" to begin the proof.  If
% the proof ends with a displayed equation, then "\end{proof}" would
% normally place the symbol one line below the display, which looks
% odd. To place the symbol on the same line as the display, add
% "\qedhere" at the end of the display.

\newtheorem{definition}{Definition}
\newtheorem{example}{Example}
\newtheorem{lemma}{Lemma}
\newtheorem{corollary}{Corollary}
\newtheorem{theorem}{Theorem}

\usepackage[algo2e,ruled,linesnumbered,noend,vlined]{algorithm2e}
\SetKwInput{KwVars}{Variables}

% For remarks:
\newcounter{remark}[section]
\renewcommand{\theremark}{\thesection.\arabic{remark}}
\newenvironment{remark}[1]%
{\par\addvspace{5pt}%
 \refstepcounter{remark}%
 \noindent{\bfseries Remark~\theremark\ (#1)}\hspace{0.5em}}%
{\par}

%----------------------------------------------------------------------
% Miscellaneous:

\newcommand{\eat}[1]{}

\RequirePackage{soul}
\setuldepth{x}
\newcommand{\reminder}[1]{\hl{[[[#1]]]}}

% An environment for making timelines: TO BE COMPLETED.

%----------------------------------------------------------------------
% Database related:

% Relational algebra:
\newcommand{\intersect}{\ensuremath{\cap}}
\newcommand{\union}{\ensuremath{\cup}}
\newcommand{\diff}{\ensuremath{-}}
\newcommand{\cross}{\ensuremath{\times}}
\newcommand{\project}{\ensuremath{\pi}}
\newcommand{\rename}{\ensuremath{\rho}}
\newcommand{\select}{\ensuremath{\sigma}}
% Long joins:
% Note that since em/ex do not scale with current math font size,
% there is no way to make the following commands work for subscript.
% Thus we have forced normal size by mbox.
%\newcommand{\join}{\ensuremath%
%  {\mathbin{\mbox{$\rhd\hspace*{-0.25em}\lhd$}}}}
%\newcommand{\semijoin}{\ensuremath%
%  {\mathbin{\mbox{$\rhd\hspace*{-0.525em}<$}}}}
%\newcommand{\rsemijoin}{\ensuremath%
%  {\mathbin{\mbox{$>\hspace*{-0.525em}\lhd$}}}}
%\newcommand{\outerjoin}{\ensuremath%
%  {\mathbin{\mbox{%
%  $\sqsupset\hspace*{-0.49em}\rhd\hspace*{-0.25em}\lhd%
%  \hspace*{-0.51em}\sqsubset$}}}}
%\newcommand{\louterjoin}{\ensuremath%
%  {\mathbin{\mbox{%
%  $\sqsupset\hspace*{-0.49em}\rhd\hspace*{-0.25em}\lhd$}}}}
%\newcommand{\routerjoin}{\ensuremath%
%  {\mathbin{\mbox{%
%  $\rhd\hspace*{-0.25em}\lhd\hspace*{-0.51em}\sqsubset$}}}}
% short joins (they scale):
%\newcommand{\join}{\ensuremath{\mathbin{\Join}}}
\newcommand{\join}{\ensuremath{\mathbin{\bowtie}}}
\newcommand{\semijoin}{\ensuremath{\mathbin{\ltimes}}}
\newcommand{\rsemijoin}{\ensuremath{\mathbin{\rtimes}}}
% Derived from above:
\newcommand{\antisemijoin}{\ensuremath{\mathbin{\overline{\semijoin}}}}
\newcommand{\rantisemijoin}{\ensuremath{\mathbin{\overline{\rsemijoin}}}}

% Bag algebra:
\newcommand{\bagunion}{\ensuremath{\uplus}}
% Note that since em/ex do not scale with current math font size,
% there is no way to make the following commands work for subscript.
% Thus we have forced normal size.
\newcommand{\bagdiff}{\ensuremath%
  {\mathbin{\mbox{\hspace*{0.25em}\raisebox{0.5ex}{$\cdot$}%
                  \hspace*{-0.52em}$-$}}}}

% View maintenance:
\newcommand{\ins}{\ensuremath{\mathord{\bigtriangleup}}}
\newcommand{\del}{\ensuremath{\mathord{\bigtriangledown}}}
% Note that since em/ex do not scale with current math font size,
% there is no way to make the following commands work for subscript.
% Thus we have forced normal size.
\newcommand{\insu}{\ensuremath%
  {\mathord{\mbox{\hspace*{0.25em}\raisebox{0.5ex}{{\tiny$\mu$}}%
                  \hspace*{-0.62em}$\bigtriangleup$}}}}
\newcommand{\delu}{\ensuremath%
  {\mathord{\mbox{\hspace*{0.25em}\raisebox{0.5ex}{{\tiny$\mu$}}%
                  \hspace*{-0.62em}$\bigtriangledown$}}}}

% Dependencies:
\newcommand{\fd}{\ensuremath{\rightarrow}}
\newcommand{\mvd}{\ensuremath{\twoheadrightarrow}}

% SQL commands:
\newcommand{\sql}[1]{\texttt{#1}}
\newenvironment{SQL}%
  {\ttfamily\begin{tabbing}\ \ \=\ \ \=\ \ \=\ \ \=\ \ \=\ \ \=\ \ \=\kill}%
  {\end{tabbing}}
